/**
 * Created by python on 19-4-13.
 */
var app = new Vue({
    el: "#app",

    data: {
        host: host,
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        avatar_url: sessionStorage.avatar_url || localStorage.avatar_url,
        token: sessionStorage.token || localStorage.token,
        nick_name: sessionStorage.nick_name || localStorage.nick_name,
        // 页面中需要使用到的数据，键值对
        news: [],
        categories: [],
        ques: [],
        activies: [],
        active: null,
        ind: [],
        catedata: [],
        len: null,
    },
    mounted: function () {
        this.start();
        this.queslist();
        this.activieslist();

    },
    methods: {

        start: function () {
            // 一加载就需要做的，直接是代码
            axios.get(host + "/news/")
                .then(response => {
                    this.news = response.data
                    // this.ind = response.data[0]['category'].id
                    // console.log(response.data[0]['category'].id)
                    // console.log(this.news.length)
                    app.len = app.news.length


                })
                .catch(error => {
                })
            axios.get(host + "/category/")
                .then(response => {

                    this.categories = response.data

                    // console.log(this.categories)
                })
                .catch(error => {
                })


        },

        queslist: function () {
            axios.get(host + "/newsques/")
                .then(response => {

                    this.ques = response.data
                    // console.log(this.ques)
                }).catch(error => {
            })
        },

        activieslist: function () {
            axios.get(host + "/newsactivies/")
                .then(response => {

                    this.activies = response.data
                    console.log(this.activies)
                }).catch(error =>{
            })
        },

        logout: function () {
            sessionStorage.clear();
            localStorage.clear();
            location.href = './person-loginsign.html';
        },

        // 需要用到的函数，键值对 ，键是名称，值是匿名函数
        toggle(index) {
            app.active = index;
            // console.log(index)


            axios.get(host + '/news/')
                .then(response => {
                    app.ind = response.data[index]['category'].id   // 获取当前鼠标点击下标对应的分类ｉｄ
                    app.catedata = response.data
                    if (index == 0) {
                        app.news = app.catedata
                    }
                    else {
                        console.log(app.catedata)
                        app.catedata = []

                        for (var i = 0; i < app.len; i++) {
                            app.ind = response.data[i]['category'].id  //　遍历每篇文章的category.id
                            // 如果文章分类ｉｄ等于鼠标点击值对应的分类ｉｄ
                            if (app.ind == index + 3) {
                                app.catedata.push(response.data[i])
                            }
                        }
                    }
                    app.news = app.catedata
                })
                .catch(error => {
                });

        },
    }
})



